

package com.gc.server.service.impl;
import com.gc.server.dao.LinkHeadDao;
import com.gc.server.dao.UserDao;
import com.gc.server.entity.LinkHeadEntity;
import com.gc.server.entity.UserEntity;
import com.yf.response.RRException;
import org.springframework.transaction.annotation.Transactional;
import com.gc.server.vo.up.UserHeadUp;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import  com.gc.server.vo.down.UserHeadDown;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.net.URLEncoder;
import com.gc.server.vo.down.UserHeadDown;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;
import com.gc.server.dao.UserHeadDao;
import com.gc.server.entity.UserHeadEntity;
import com.gc.server.vo.UserHeadVo;
import com.gc.server.service.UserHeadService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date ${datetime}
 */
@Service
public class UserHeadServiceImpl extends BaseServiceImpl<UserHeadEntity> implements UserHeadService {

    private UserHeadDao dao;


    @Autowired
    public void setDao(UserHeadDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }




    @Override
    public List<UserHeadVo> findVo(UserHeadVo vo){
        return dao.findVo(vo);
    }

    @Override
    public UserHeadVo getVo(Long id){
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(UserHeadVo vo){
        UserEntity userEntity = userDao.findByCode(vo.getUser().getCode());
        LinkHeadEntity headEntity = linkHeadDao.findByCode(vo.getHead().getCode());
        if(userEntity==null||headEntity==null){
            throw new RRException("编号数据异常");
        }
        vo.getEntity().setUserId(userEntity.getId());
        vo.getEntity().setHeadId(headEntity.getId());
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(UserHeadVo vo){
        return dao.update(vo.getEntity());
    }



    @Override
    public void downLoad(HttpServletResponse response, List<UserHeadVo> list) {
        try {
            List<UserHeadDown> datas = list.stream().map(item -> new UserHeadDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel" );
            response.setCharacterEncoding("utf-8" );
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8" );
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx" );
            EasyExcel.write(response.getOutputStream(), UserHeadDown.class).sheet("模板" ).doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Autowired
    private UserDao userDao;

    @Autowired
    private LinkHeadDao linkHeadDao;

    @Override
    public void upload(InputStream file) {
        EasyExcel.read(file, UserHeadUp.class, new UploadDataListener()).sheet().doRead();
    }

    class UploadDataListener extends AnalysisEventListener<UserHeadUp> {


        @Override
        @Transactional(rollbackFor = Exception.class)
        public void invoke(UserHeadUp upVo, AnalysisContext analysisContext) {
            saveVo(upVo.toVo());
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

    }




    /**内部方法**/



    /**外部方法实现**/




}
